const query=require("../db/mysql")
const {success,error}=require("../modle/modle")
class Homeconunt{
    //获取商品列表
   getList=async (ctx)=>{
    const {pageSize,pageIndex,keyword,sortBy,sortType}=ctx.query;
    let sql=`select * from shops where 1=1 `
    let sqlCount=`select count(id) as total from shops where 1=1 `
    if(keyword){
       sql+=`and title like "%${keyword}%"`
       sqlCount+=`and title like "%${keyword}%"`
    }
    if(sortBy&&sortType){
        sql+=`order by ${sortBy} ${sortType}`
    }
    if(pageSize&&pageIndex){
        sql+=`limit ${(pageIndex-1)*pageSize},${pageSize}`
    }
    const data=await query(sql)
    console.log(data);
    
    const count=await query(sqlCount)
    ctx.body=success({
        data,
        total:count[0].total
    })

   }

   //商品详情
   detailList=async (ctx)=>{
      const {id}=ctx.params;
      let sql=`select * from shops where id=${id}`
      const data=await query(sql)
      ctx.body=success(data)
   }
}
module.exports=new Homeconunt()